package com.supermap.search.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.supermap.greendao.CollectBeanDao;
import com.supermap.search.R;
import com.supermap.search.adapter.SwipeCollectAdapter;
import com.supermap.search.javabean.CollectBean;
import com.supermap.search.sys.loc.LocationApplication;
import com.supermap.search.utils.ToastUtils;
import com.supermap.search.utils.Utils;
import com.supermap.search.wigdet.SwipeLayoutManager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 未上传采集的界面
 * Created by XueQing Wang on 2018/6/29.
 */

public class NoUpCollectActivity extends AppCompatActivity {
    @BindView(R.id.tv_allclear)
    TextView tvAllclear;
    @BindView(R.id.lv_weishangchuan)
    ListView lvWeishangchuan;


    //初始化巡检dao
    private CollectBeanDao dao;
    private List<CollectBean> collectBeanList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_noshangchuancaiji);
        ButterKnife.bind(this);
        initdata();
    }

    private void initdata() {
        dao = LocationApplication.getInstance().getDaoSession().getCollectBeanDao();
        collectBeanList = dao.loadAll();
        if (collectBeanList != null && collectBeanList.size() > 0) {
            //不為空
            lvWeishangchuan.setVisibility(View.VISIBLE);
            tvAllclear.setVisibility(View.GONE);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    SwipeCollectAdapter adapter = new SwipeCollectAdapter(collectBeanList, Utils.getContext());
                    adapter.setOnClickListener(new SwipeCollectAdapter.OnClickListener() {
                        @Override
                        public void onDeleteClick(int position) {
                            dao.delete(collectBeanList.get(position));
                            collectBeanList.remove(position);
                            adapter.notifyDataSetChanged();
                            ToastUtils.showShortToast("删除成功");
                        }

                        @Override
                        public void onUploadClick(int position) {

                        }
                    });
                    lvWeishangchuan.setAdapter(adapter);
                }
            }).start();

        } else {
            lvWeishangchuan.setVisibility(View.GONE);
            tvAllclear.setVisibility(View.VISIBLE);
        }

    }

    @Override
    protected void onPause() {
        SwipeLayoutManager.getInstance().clearCurrentLayout();
        super.onPause();
    }
}
